# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-concurrent-ruby
_gemname=${pkgname#ruby-}
pkgver=1.1.6
pkgrel=0
pkgdesc="Modern concurrency tools for Ruby"
url="http://www.concurrent-ruby.com"
# mips, mips64: requires 'jruby' and 'openjdk8'
# x86, armv7, ppc64le: requires 'java-jffi-native'
arch="all !mips !mips64 !x86 !armv7 !ppc64le"
license="MIT"
depends="ruby ruby-etc"
makedepends="jruby openjdk8 ruby-dev ruby-rake ruby-rake-compiler"
checkdepends="ruby-rspec ruby-timecop"
subpackages="$pkgname-java::noarch $pkgname-ext"
source="$pkgname-$pkgver.tar.gz::https://github.com/ruby-concurrency/$_gemname/archive/v$pkgver.tar.gz
	ungit-gemspec.patch
	rakefile-remove-dock.patch
	"
builddir="$srcdir/$_gemname-$pkgver"

case "$CARCH" in
	# XXX: Two test fail, see https://github.com/ruby-concurrency/concurrent-ruby/issues/689
	armhf) options="!check";;
esac

build() {
	export PATH="/usr/lib/jvm/java-1.8-openjdk/bin:$PATH"
	export JRUBY_HOME="/usr/share/jruby"

	rake repackage

	RUBYLIB="$(pwd)/lib" gem install --local \
		--install-dir dist-ext \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-ext-$pkgver.gem
}

check() {
	gem install --local \
		--install-dir .gems \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-*.gem

	GEM_PATH=".gems" rspec --tag ~unfinished --tag ~buggy --tag ~edge spec
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	gem install --local \
		--install-dir "$gemdir" \
		--ignore-dependencies \
		--no-document \
		--verbose \
		pkg/$_gemname-$pkgver.gem

	# Remove unnecessary files.
	rm -r "$gemdir"/cache \
		"$gemdir"/build_info \
		"$gemdir"/doc
	cd "$gemdir"/gems/$_gemname-$pkgver
	rm Gemfile Rakefile *.md
}

java() {
	pkgdesc="$pkgdesc (Java extension)"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel jruby"

	amove $(ruby -e 'puts Gem.default_dir')/gems/$_gemname-$pkgver/ext
}

ext() {
	pkgdesc="$pkgdesc (C extension)"
	depends="$pkgname=$pkgver-r$pkgrel"

	local gemdir="$subpkgdir/$(ruby -e 'puts Gem.default_dir')"

	cd "$builddir"/dist-ext

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete
	cd "$gemdir"/gems/$_gemname-ext-$pkgver
	rm -r ext/ lib/concurrent/*.so *.md
	rmdir -p --ignore-fail-on-non-empty "$(pwd)/lib/concurrent"
}

sha512sums="45b841761ab7060e437b334f8a1a6bcc60e230483cbf3176d6224020b6f7232fe61b0364a7ebe95b80e895847675a5ba0ae075bafcc35b709a60e288c32f2db3  ruby-concurrent-ruby-1.1.6.tar.gz
4f464764072f8870a99c7b1ea263993f95e98edaf1c7f7ad350321ab74676d6511659eed779786d5dc9d31eae83c45651fc4a8895ea7082a7ebcf29398dd05d4  ungit-gemspec.patch
964384305dbe7d0578e80117587235acd2ab8e812aaa9353a13157ad92574b6db632fe96b144950e64377f94af26fa2ec1bad1c829493c48d379c0c91e1282b7  rakefile-remove-dock.patch"
